<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminIderController extends Controller
{
    //意见列表
    public function getIndex()
    {
        //查询所有意见
        $data = DB::table('ider')->paginate(10);
        //解析模板
        return view('admin.ider.index',['ider'=>$data]);
    }
    //修改
    public function getEdit($id)
    {
        $data = DB::table('ider')->where('id',$id)->first();

        //解析模板
        return view('admin.ider.edit',['ider'=>$data]);
    }

   //执行修改
    public function postUpdate(Request $request)
    {
        //接收数据
        $data = $request->only(['title','content','uname','status','show']);
        $data1 = $request->only(['title1','content1','uname1','status1','show1']);

        //判断是否相等
        if(array_values($data) == array_values($data1)){
            return redirect('/admin/ider/index')->with('success','修改成功');
        }else{
            //修改数据
            $res = DB::table('ider')->where('id',$request->input('id'))->update($data);
            //判断
            if($res){
                return redirect('/admin/ider/index')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
        }
    }

    //删除从
    public function postDelete(Request $request)
    {
        $id = $request->input('id');
        //查询有没有回帖
        $dd = DB::table('huifu')->where('iid',$id)->first();
        //判断
        if($dd){
            return 2;
        }
        //删除
        $res = DB::table('ider')->where('id',$id)->delete();
        //判断
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    //回复列表
    public function getHuifuindex()
    {
        //查询
        $data = DB::table('huifu')
        ->join('ider','huifu.iid','=','ider.id')
        ->select('huifu.*','ider.title')
        ->paginate(10);
        //解析模板
        return view('admin.ider.huifuindex',['huifu'=>$data]);
    }

    //回复修改
    public function getHuifuedit($id)
    {
        // 查询数据
        $res = DB::table('huifu')
        ->join('ider','huifu.iid','=','ider.id')
        ->select('huifu.*','ider.title')
        ->where('huifu.id',$id)
        ->first();

        //解析模板
        return view('admin.ider.huifuedit',['huifu'=>$res]);
    }

    //执行回复修改
    public function postHuifuupdate(Request $request)
    {
        //接收数据
        $data = $request->only(['content']);
        $data1 = $request->only(['content1']);
        //判断
        if(array_values($data) == array_values($data1))
        {
            return redirect('/admin/ider/huifuindex')->with('success','修改成功');
        }else{
            $res = DB::table('huifu')->where('id',$request->input('id'))->update($data);
            //判断
            if($res){
                return redirect('/admin/ider/huifuindex')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
        }
    }

    //回复删除
    public function postHuifudelete(Request $request)
    {
        //删除
        $res = DB::table('huifu')->where('id',$request->input('id'))->delete();
        //判断
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
}
